package com.ywu.bpmn.standard.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.bpmn.spi.dto.YwuBpmnDto;
import com.ywu.bpmn.spi.dto.YwuBpmnMqDto;
import com.ywu.bpmn.standard.produce.YwuBpmnProduce;
import com.ywu.common.gson.GsonUtils;
import com.ywu.common.result.CommonResult;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName YwuActivitiService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/4/23 10:49
 * @Version 1.0
 **/
@Service
public class YwuActivitiService {

    @Autowired
    private YwuBpmnProduce produce;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    private static final String PROCESS_INSTANCE_START_KEY = "start";

    /**
     * @Author GroundDemo
     * @Description 部署流程
     * @Date 11:15 2025/4/23
     * @Param [dto]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    public CommonResult<Object> deployFlow(YwuBpmnDto dto) {
        Deployment deploy = repositoryService.createDeployment().key(dto.getKey())
                .name(dto.getName())
                .addString(dto.getKey() + ".bpmn", dto.getBpmn())
                .deploy();
        return CommonResult.success(deploy);
    }

    /**
    * @Author GroundDemo
    * @Description dto
    * @Date 11:10 2025/4/23
    * @Param [dto]
    * @return void
    **/
    @Async
    public void start(YwuBpmnDto dto, YwuContext currentContext) {
        YwuContextHolder.setCurrentContext(currentContext);
        if (CollectionUtils.isEmpty(dto.getVariable())) {
            runtimeService.startProcessInstanceByKey(dto.getKey());
        } else {
            runtimeService.startProcessInstanceByKey(dto.getKey(), dto.getVariable());
        }
    }

    /**
    * @Author GroundDemo
    * @Description 查询用户代办列表
    * @Date 11:16 2025/4/23
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> toDoList(YwuBpmnDto dto) {
        List<Task> test111 = taskService.createTaskQuery().taskAssignee(dto.getAssigneeName()).list();
        return CommonResult.success(test111.toString());
    }

    /**
    * @Author GroundDemo
    * @Description 用户审批
    * @Date 11:19 2025/4/23
    * @Param [dto]
    * @return void
    **/
    @Async
    public void complete(YwuBpmnDto dto, YwuContext currentContext) {
        YwuContextHolder.setCurrentContext(currentContext);
        taskService.complete(dto.getTaskId());
    }
}
